package cn.ac.aircas.web.controller.common;

import cn.ac.aircas.common.annotation.Anonymous;
import cn.ac.aircas.framework.config.OAuth2Config;
import cn.ac.aircas.common.constant.Constants;
import cn.ac.aircas.common.core.controller.BaseController;
import cn.ac.aircas.common.core.domain.AjaxResult;
import cn.ac.aircas.common.core.domain.entity.LoginByOtherSourceBody;
import cn.ac.aircas.common.exception.ServiceException;
import cn.ac.aircas.common.utils.uuid.IdUtils;
import cn.ac.aircas.framework.config.SocialUtils;
import cn.ac.aircas.framework.config.properties.SocialLoginConfigProperties;
import cn.ac.aircas.framework.web.service.SysLoginService;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.request.AuthGiteeRequest;
import me.zhyd.oauth.request.AuthGithubRequest;
import me.zhyd.oauth.request.AuthRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@Slf4j
@RestController
public class OAuth2Controller extends BaseController {
    @Autowired
    private SysLoginService sysLoginService;

    @Autowired
    private OAuth2Config oAuth2Config;

    @GetMapping("PreLoginByGitee")
    public AjaxResult PreLoginByGitee() {
        SocialLoginConfigProperties loginConfigProperties = oAuth2Config.getType().get("gitee");
        AjaxResult ajax = AjaxResult.success();
        AuthRequest authRequest = new AuthGiteeRequest(AuthConfig.builder()
                .clientId(loginConfigProperties.getClientId())
                .clientSecret(loginConfigProperties.getClientSecret())
                .redirectUri(loginConfigProperties.getRedirectUri())
                .build());
        String uuid = IdUtils.fastUUID();
        String authorizeUrl = authRequest.authorize(uuid);
        //存储
        ajax.put("authorizeUrl", authorizeUrl);
        ajax.put("uuid", uuid);
        ajax.put("source", "gitee");
        return ajax;
    }

    @Anonymous
    @GetMapping("PreLoginByGitHub")
    public AjaxResult PreLoginByGitHub() {
        SocialLoginConfigProperties loginConfigProperties = oAuth2Config.getType().get("github");
        AjaxResult ajax = AjaxResult.success();
        AuthRequest authRequest = new AuthGithubRequest(AuthConfig.builder()
                .clientId(loginConfigProperties.getClientId())
                .clientSecret(loginConfigProperties.getClientSecret())
                .redirectUri(loginConfigProperties.getRedirectUri())
                .build());
        String uuid = IdUtils.fastUUID();
        String authorizeUrl = authRequest.authorize(uuid);
        //存储
        ajax.put("authorizeUrl", authorizeUrl);
        ajax.put("uuid", uuid);
        ajax.put("source", "github");
        return ajax;
    }

    @Anonymous
    @GetMapping("PreLoginByThirdPlat")
    public AjaxResult PreLoginByThirdPlat(@RequestParam String source) {
        AjaxResult ajax = AjaxResult.success();
        AuthRequest authRequest = SocialUtils.getAuthRequest(source, oAuth2Config);
        String uuid = IdUtils.fastUUID();
        String authorizeUrl = authRequest.authorize(uuid);
        //存储
        ajax.put("authorizeUrl", authorizeUrl);
        ajax.put("uuid", uuid);
        ajax.put("source", source);
        return ajax;
    }

    @PostMapping("/loginByGitee")
    public AjaxResult loginByGitee(@RequestBody LoginByOtherSourceBody loginByOtherSourceBody) throws IOException {
        AjaxResult ajax = AjaxResult.success();
        String token = sysLoginService
                .loginByOtherSource(loginByOtherSourceBody.getCode(), loginByOtherSourceBody.getSource(), loginByOtherSourceBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        if (token == null) {
            throw new ServiceException("第三方授权登录失败，请重新授权",4401);
        }
        return ajax;
    }

    @Anonymous
    @PostMapping("/loginByGitHub")
    public AjaxResult loginByGitHub(@RequestBody LoginByOtherSourceBody loginByOtherSourceBody) throws IOException {
        AjaxResult ajax = AjaxResult.success();
        String token = sysLoginService
                .loginByGitHubSource(loginByOtherSourceBody.getCode(), loginByOtherSourceBody.getSource(), loginByOtherSourceBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        if (token == null) {
            throw new ServiceException("第三方授权登录失败，请重新授权",4401);
        }
        return ajax;
    }

    @Anonymous
    @PostMapping("/loginByThirdPlat")
    public AjaxResult loginByThirdPlat(@RequestBody LoginByOtherSourceBody loginByOtherSourceBody) throws Exception {
        AjaxResult ajax = AjaxResult.success();
        String token = sysLoginService
                .loginByThirdPlatSource(loginByOtherSourceBody.getCode(), loginByOtherSourceBody.getSource(), loginByOtherSourceBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        if (token == null) {
            throw new ServiceException("第三方授权登录失败，请重新授权",4403);
        }
        return ajax;
    }

    @Anonymous
    @PostMapping("/loginByWeibo")
    public AjaxResult loginByWeibo(@RequestBody LoginByOtherSourceBody loginByOtherSourceBody) {
        AjaxResult ajax = AjaxResult.success();
        String token = sysLoginService.loginByThirdPlatformSource(loginByOtherSourceBody.getCode(), loginByOtherSourceBody.getSource(), loginByOtherSourceBody.getUuid());

        ajax.put(Constants.TOKEN, token);
        if (token == null) {
            throw new ServiceException("第三方授权登录失败，请重新授权",4403);
        }
        return ajax;
    }
}